﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DefCore.Interfaces;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;

namespace DefCore.AI
{
    /*  return -1: ignore this fixture and continue
return 0: terminate the ray cast
return fraction: clip the ray to this point
return 1: don't clip the ray and continue
     */
    public class Raycaster
    {
        //Fields
        private IRayCaster _Caster;
        private bool _InternalLOSCheck { get; set; }

        //ctor  
        public Raycaster(IRayCaster caster)
        {
            _Caster = caster;
        }

        //Methods
        private float LoSCheck(Fixture fixture, Vector2 position, Vector2 normal, float fraction)
        {
            _InternalLOSCheck = true;
            LOSCollisionCategory cat = (LOSCollisionCategory)fixture.UserData;

            if (cat == LOSCollisionCategory.Unit)
                return -1;
            else if (cat == LOSCollisionCategory.Wall)
            {
                _InternalLOSCheck = false;
                return 1;
            }
            else
                return fraction;
        }

        public bool CanSee(Vector2 position)
        {
            //Can A See B
            _InternalLOSCheck = false;
            Objects.GameWorld.RayCast(LoSCheck, _Caster.RayOrigin, position);
            return _InternalLOSCheck;
        }
    }
}
